package com.thenie.controller;

import com.alibaba.fastjson.JSONObject;
import com.thenie.pojo.Floor;
import com.thenie.pojo.Room;
import com.thenie.pojo.RoomType;
import com.thenie.pojo.User;
import com.thenie.service.FloorService;
import com.thenie.service.RoomService;
import com.thenie.service.RoomTypeService;
import com.thenie.service.UserService;
import com.thenie.utils.SystemConstant;
import com.thenie.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

import static com.thenie.utils.SystemConstant.LOGINUSER;

@Controller
public class IndexController {
    @Resource
    private RoomTypeService roomTypeService;
    @Resource
    private FloorService floorService;
    @Resource
    private RoomService roomService;
    @Autowired
    private UserService userService;
    @RequestMapping("/index.html")
    public String index(Model model){
        HashMap<String, Object> map = new HashMap<String, Object>();
        //查询房型列表
        List<RoomType> roomTypeList = roomTypeService.findRoomTypeList(new RoomTypeVo());
        //查询楼层列表
        List<Floor> floorList = floorService.findFloorList(new FloorVo());

        for (Floor floor:floorList){//根据楼层的Id查询房间的信息（包括价格）
            Integer floorId = floor.getId();//楼层Id
            String floorName = floor.getName();//楼层的名称
            //根据楼层查询房间
            List<Room> roomByFloorId = roomService.findRoomByFloorId(floorId);
            map.put(floorName,roomByFloorId);
        }
        model.addAttribute("roomTypeList", roomTypeList);
        model.addAttribute("floorList",floorList);
        model.addAttribute("roomMap", map);
        return "forward:/home.jsp";
    }

    @RequestMapping("/login.html")
    public String login(){
        return "forward:/login.jsp";
    }

    @RequestMapping("/register.html")
    public String register(){
        return "forward:/register.jsp";
    }

    @RequestMapping("/yud.html")
    public String yud(Model model,HttpSession session){
        User user = (User)session.getAttribute(SystemConstant.LOGINUSER);
        if (null ==user){
            return "forward:/index.jsp";
        }

        RoomVo roomVo = new RoomVo();
        roomVo.setLoginName(user.getLoginName());
        List<Room> roomList = roomService.findRoomList(roomVo);

        model.addAttribute("roomList",roomList);
        return "forward:/yud.jsp";
    }


    @RequestMapping("/userinfo.html")
    public String userinfo(Model model,HttpSession session){
        User loginUser = (User)session.getAttribute(    LOGINUSER);
        User info = userService.findUserByLoginName(loginUser.getLoginName());
        model.addAttribute("info",info);
        System.out.println(info.getLoginName());
        return "forward:userinfo.jsp";
    }

    @RequestMapping("/room/info")
    @ResponseBody
    public String roomInfo(Integer roomId){
        Room room = roomService.roomInfo(roomId);
        room.setStatusName(RoomStatusEnum.getByValue(room.getStatus()).getLabel());

        return JSONObject.toJSONString(room);
    }

    @RequestMapping("/room/updateRoom")
    @ResponseBody
    public String updateRoom(Room room, HttpSession session){
        User user = (User) session.getAttribute(SystemConstant.LOGINUSER);
        HashMap<String, Object> map = new HashMap<String, Object>();
        System.out.println(user);

        if (user == null){
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"请登录！！！");
            return JSONObject.toJSONString(map);
        }

        Room info = new Room();
        info.setId(room.getId());
        info.setUserMsg(room.getUserMsg1());
        info.setUserName(room.getUserName1());
        info.setLoginName(user.getLoginName());
        info.setStatus(RoomStatusEnum.RESERVED.getValue());

        int i = roomService.updateRoom(info);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"预定成功！！！");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"预定失败！！！");
        }
        return JSONObject.toJSONString(map);
    }

    @RequestMapping("/room/roll")
    @ResponseBody
    public String updateRoom(Room room){
        HashMap<String, Object> map = new HashMap<String, Object>();

        Room info = new Room();
        info.setId(room.getId());
        info.setUserMsg("");
        info.setUserName("");
        info.setLoginName("");
        info.setStatus(RoomStatusEnum.IDLE.getValue());

        int i = roomService.updateRoom(info);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"取消预定成功！！！");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"取消预定失败！！！");
        }
        return JSONObject.toJSONString(map);
    }

}
